package com.itheima.tlias.controller;


import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.config.OssClient;
import com.itheima.tlias.utils.TXCOSUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {
    @Resource
    private OssClient ossClient;

    /*@PostMapping
    public Result upload(MultipartFile file){
        String path = TXCOSUtil.upload(file);
        log.info("path={}",path);
        return Result.success();
    }*/
    @PostMapping
    public Result upload(MultipartFile file){
        //获取参数名
        log.info("name{}",file.getName());
        //获取实际的上传文件名
        log.info("originalName={}",file.getOriginalFilename());
        //拆分文件后缀（类型）
        String filename = file.getOriginalFilename();
        try {
            String url = ossClient.upload(filename, file.getInputStream());
            return Result.success("success",url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
